package com;

import com.wn.bean.Account;
import com.wn.mapper.AccountMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author yellow Docter
 * @date 2022 -07-08
 * @desc
 *
 *  spring 整合mybatis
 *
 *  01 搭建环境 数据库
 *  02 依赖
 *  03 写配置
 *     数据库配置
 *     mybatis-config.xml.xml的配置
 *
 */
public class App {

    public static void main(String[] args) throws IOException {

        // 测试拿到mapper
        //01  sqlSessionFactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //02  sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //03  mapper
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);

        //04 执行方法
        Account account = mapper.findAccountById(1);

        System.out.println("account = " + account);

        if (sqlSession != null) {
            sqlSession.close();
        }

    }
}
